package com.nowcoder.community.util;

import com.nowcoder.community.entity.User;
import org.springframework.stereotype.Component;

/**
 * @ClassName HostHolder
 * @Description
 * @Author hhwu
 * @Date 2022/5/20 16:32
 * Version 1.0
 **/
@Component
public class HostHolder {
    /**
     * @Author hhwu
     * @Description //TODO 持有用户的信息，用于代替session对象。将某个对象存在单个线程中，防止多线程时冲突
     * @Date 16:34 2022/5/20
     **/
    private ThreadLocal<User> users = new ThreadLocal<>();

    public void setUser(User user) {
        users.set(user);
    }

    public User getUser() {
        return users.get();
    }

    public void clear() {
        /**
         * @Author hhwu
         * @Description //TODO 对象用完后要清理
         * @Date 16:46 2022/5/20
         * @Param []
         * @return void
         **/
        users.remove();
    }
}

